﻿using System;
using MyHealthByDesign.Domain;

namespace MyHealthByDesign.Views
{
    /// <summary>
    /// Description:
    /// Displays health gram page
    /// 
    /// Author: Sanjeev Shrestha
    /// Date Created: 10/31/2011
    /// 
    /// Other code files referenced:
    /// None.
    /// 
    /// Regression Test Information:
    /// None.
    /// 
    /// Revision History:
    /// 11/04/2011 Sanjeev Shrestha - Added logic for health gram data calculations.
    /// 11/06/2011 Sanjeev Shrestha - Bug Fixes.
    /// 
    /// 
    /// Known Faults:
    /// None.
    /// </summary>
    public partial class HealthGram : System.Web.UI.Page
    {
        Domain.Domain domain = new Domain.Domain();

        /// <summary>
        /// Page Load event handler - check if user is logged in
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Page displayed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!domain.IsUserLoggedIn())
            {
                Response.Redirect("../Default.aspx");
            }

            GetUserDetail();
        }

        /// <summary>
        /// GetUserDetail - populate from user answers
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Form elements are poulated
        /// </summary>
        private void GetUserDetail()
        {
            if (Session["User"] != null)
            {
                var user = (User)Session["User"];
                lblName.Text = user.Firstname + " " + user.Lastname;
                lblSex.Text = user.Gender;
                var birthDate = Convert.ToDateTime(user.BirthDate);
                lblAge.Text = domain.GetAge(birthDate).ToString() +" yrs";

                HealthMeasurement healthMeasurement = domain.GetUserHealthMeasurement(user);
                lblHeight.Text = healthMeasurement.Height > 0 ? healthMeasurement.Height.ToString() +" in" : "N/A";
                lblWeight.Text = healthMeasurement.Weight > 0 ? healthMeasurement.Weight.ToString() + " lbs": "N/A";

                if (healthMeasurement.SystolicBp > 0)
                {
                    lblSystolic.Text = healthMeasurement.SystolicBp.ToString();
                    ltrSystolicStatus.Text = GetSystolicStatus(healthMeasurement.SystolicBp);
                }
                else
                {
                    lblSystolic.Text = "N/A";
                    ltrSystolicStatus.Text = "<td>N/A</td>";
                }

                if (healthMeasurement.DiastoliBp > 0)
                {
                    lblDiastolic.Text = healthMeasurement.DiastoliBp.ToString();
                    ltrDiastolicStatus.Text = GetDiastolicStatus(healthMeasurement.DiastoliBp);
                }
                else
                {
                    lblDiastolic.Text = "N/A";
                    ltrDiastolicStatus.Text = "<td>N/A</td>";
                }

                if (healthMeasurement.WaistMeasurement > 0)
                {
                    lblWaistMeasurement.Text = healthMeasurement.WaistMeasurement.ToString();
                    ltrWaistMeasurement.Text = GetWaistMeasurementStatus(healthMeasurement.WaistMeasurement, user.Gender);
                }
                else
                {
                    lblWaistMeasurement.Text = "N/A";
                    ltrWaistMeasurement.Text = "<td>N/A</td>";
                }

                if (healthMeasurement.Bmi > 0)
                {
                    lblBmi.Text = healthMeasurement.Bmi.ToString();
                    ltrBmi.Text = GetBmiStatus(healthMeasurement.Bmi);
                }
                else
                {
                    lblBmi.Text = "N/A";
                    ltrBmi.Text = "<td>N/A</td>";
                }

                if (healthMeasurement.TotalCholesterol > 0)
                {
                    lblTotalCholesterol.Text = healthMeasurement.TotalCholesterol.ToString();
                    ltrTotalCholesterol.Text = GetTotalCholesterolStatus(healthMeasurement.TotalCholesterol);
                }
                else
                {
                    lblTotalCholesterol.Text = "N/A";
                    ltrTotalCholesterol.Text = "<td>N/A</td>";
                }

                if (healthMeasurement.BloodGlucose > 0)
                {
                    lblBloodGlucose.Text = healthMeasurement.BloodGlucose.ToString();
                    ltrBloodGlucose.Text = GetBloodGlucoseStatus(healthMeasurement.BloodGlucose);
                }
                else
                {
                    lblBloodGlucose.Text = "N/A";
                    ltrBloodGlucose.Text = "<td>N/A</td>";
                }
            }
        }

        /// <summary>
        /// GetSystolicStatus - populate table cell based on value
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// table cells are populated
        /// </summary>
        /// <param name="systolicBp"></param>
        /// <returns></returns>
        private string GetSystolicStatus(int systolicBp)
        {
            string status;

            if (systolicBp < 120)
            {
                status = "<td class='bgGreen'>Good</td>";
            }
            else if (systolicBp < 140)
            {
                status = "<td class='bgYellow'>Needs Improvement</td>";
            }
            else
            {
                status = "<td class='bgRed'>Health Risk</td>";
            }

            return status;
        }

        /// <summary>
        /// GetDiastolicStatus - populate table cell based on value
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// table cells are populated
        /// </summary>
        /// <param name="diastolicBp"></param>
        /// <returns></returns>
        private string GetDiastolicStatus(int diastolicBp)
        {
            string status;

            if (diastolicBp < 80)
            {
                status = "<td class='bgGreen'>Good</td>";
            }
            else
            {
                status = "<td class='bgYellow'>Needs Improvement</td>";
            }

            return status;
        }

        /// <summary>
        /// GetWaistMeasurementStatus - populate table cell based on value
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// table cells are populated
        /// </summary>
        /// <param name="waist"></param>
        /// <param name="gender"></param>
        /// <returns></returns>
        private string GetWaistMeasurementStatus(double waist, string gender)
        {
            string status = null;

            if (gender == "M")
            {
                if (waist < 31.5)
                {
                    status = "<td class='bgYellow'>Very Low</td>";
                }
                else if (waist < 39)
                {
                    status = "<td class='bgGreen'>Low</td>";
                }
                else if (waist < 47)
                {
                    status = "<td class='bgYellow'>High</td>";
                }
                else
                {
                    status = "<td class='bgRed'>Very High</td>";
                }
            }

            if (gender == "F")
            {
                if (waist < 27.5)
                {
                    status = "<td class='bgYellow'>Very Low</td>";
                }
                else if (waist < 35)
                {
                    status = "<td class='bgGreen'>Low</td>";
                }
                else if (waist < 43)
                {
                    status = "<td class='bgYellow'>High</td>";
                }
                else
                {
                    status = "<td class='bgRed'>Very High</td>";
                }
            }

            return status;
        }

        /// <summary>
        /// GetBmiStatus - populate table cell based on value
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// table cells are populated
        /// </summary>
        /// <param name="bmi"></param>
        /// <returns></returns>
        private string GetBmiStatus(double bmi)
        {
            string status;

            if (bmi < 18.5)
            {
                status = "<td class='bgYellow'>Underweight</td>";
            }
            else if (bmi < 24.9)
            {
                status = "<td class='bgGreen'>Normal</td>";
            }
            else if (bmi < 30)
            {
                status = "<td class='bgYellow'>Overweight</td>";
            }
            else
            {
                status = "<td class='bgRed'>Obese</td>";
            }

            return status;
        }

        /// <summary>
        /// GetTotalCholesterolStatus - populate table cell based on value
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// table cells are populated
        /// </summary>
        /// <param name="totalCholesterol"></param>
        /// <returns></returns>
        private string GetTotalCholesterolStatus(int totalCholesterol)
        {
            string status;

            if (totalCholesterol < 200)
            {
                status = "<td class='bgGreen'>Good</td>";
            }
            else if (totalCholesterol < 239)
            {
                status = "<td class='bgYellow'>Needs Improvement</td>";
            }
            else
            {
                status = "<td class='bgRed'>Health Risk</td>";
            }

            return status;
        }

        /// <summary>
        /// GetBloodGlucoseStatus - populate table cell based on value
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// table cells are populated
        /// </summary>
        /// <param name="bloodGlucose"></param>
        /// <returns></returns>
        private string GetBloodGlucoseStatus(int bloodGlucose)
        {
            string status;

            if (bloodGlucose < 100)
            {
                status = "<td class='bgGreen'>Good</td>";
            }
            else if (bloodGlucose < 126)
            {
                status = "<td class='bgYellow'>Needs Improvement</td>";
            }
            else
            {
                status = "<td class='bgRed'>Health Risk</td>";
            }

            return status;
        }
    }
}